package com.simple.study.controller.user;

import com.simple.study.base.controller.BaseController;
import com.simple.study.entity.PageResult;
import com.simple.study.entity.ShopResult;
import com.simple.study.pojo.User;
import com.simple.study.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * @ClassName UserController
 * @Author SimpleTree
 **/
@RestController
@RequestMapping("/users")
public class UserController extends BaseController {
    @Autowired
    private UserService userService;

    /**
     * 登录验证
     * @param username
     * @return
     */
    @GetMapping("/load/{username}")
    public User findUserInfo(@PathVariable String username) {
        User user = userService.findByUsername(username);
        return user;
    }

    /**
     * 用户数据列表
     * @param query
     * @param pagenum
     * @param pagesize
     * @return
     */
    @GetMapping
    public ShopResult getUsersList(String query,Integer pagenum,Integer pagesize){
        if (pagenum == null) {
            pagenum = 0;
        }
        if (pagesize == null) {
            pagesize = 5;
        }
        PageResult<User> page = userService.getUsersList(query, pagenum, pagesize);
        Map<String, Object> map = new HashMap<>();
        map.put("total", page.getTotal());
        map.put("users", page.getRows());
        return shopSuccess(200,"获取成功",map);
    }

    /**
     * 添加用户
     * @param user
     * @return
     */
    @PostMapping
    public ShopResult addUser(@RequestBody User user) {
        return shopSuccess(201,"用户创建成功",userService.addUser(user));
    }

    /**
     * 根据id查找用户
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public ShopResult findById(@PathVariable Integer id) {
        return shopSuccess(200, "查询用户成功", userService.findById(id));
    }

    /**
     * 根据id修改用户
     * @param user
     * @return
     */
    @PutMapping("/{userId}")
    public ShopResult editUserById(@RequestBody User user,@PathVariable("userId")Integer userId) {
        return shopSuccess(200, "更新成功", userService.editUserById(user, userId));
    }

    /**
     * 根据userId删除用户
     * @param userId
     * @return
     */
    @DeleteMapping("/{userId}")
    public ShopResult deleteUserById(@PathVariable("userId") Integer userId) {
        userService.deleteUserById(userId);
        return shopSuccess(200, "删除成功", null);
    }

    /**
     * 根据userId修改用户状态
     * @param userId
     * @param user
     * @return
     */
    @PutMapping("/state/{userId}")
    public ShopResult updateUserState(@RequestBody User user,@PathVariable Integer userId) {
        userService.updateUserState(user, userId);
        return shopSuccess(200, "设置状态成功", null);
    }

    /**
     * 分配用户角色
     * @param userId
     * @param rid
     * @return
     */
    @PutMapping("/role/{userId}")
    public ShopResult distributionRole(@PathVariable("userId") Integer userId, @RequestBody User user) {
        userService.distributionRole(userId, user);
        return shopSuccess(200, "cg", null);
    }

}
